﻿package {
	import flash.display.*;
	import flash.text.*;
	import flash.utils.*;
	
	public class MusicPlayerList extends MovieClip {
		
		private var list:MPList = new MPList();
		private var current:int = 0;
		private var fields:Array = new Array();
		
		public function MusicPlayerList(titels:Array) {
			if(titels == null || titels.length == 0) {
				titels = new Array("None");
			}
			
			var f:TextField = list.item_txt;
			f.text = titels[0];
			fields.push(f);
			
			var distX = f.x - list.x;
			var distY = f.y - list.y;
			var len:Number = 2 * distY + f.height;
			for(var j:int = 1; j < titels.length; j++) {
				var f2:TextField = new TextField();
				f2.text = titels[j];
				f2.setTextFormat(f.getTextFormat());
				f2.y = len;
				f2.x = distX;
				f2.width = f.width;
				f2.height = f.height;
				list.addChild(f2);
				fields.push(f2);
				len = len + f.height + distY;
			}
			
			//list.height = len;
			addChild(list);
			mark(f);
		}
		
		public function getCurrent():String {
			return fields[current].text;
		}
		
		public function init() {
			var initX:Number = x;
			x = - 120;
			//visible = true;
			var id:Number = setInterval(moveme, 4);
			//var i:int = 0;
			function moveme() {
				if(x >= initX) {
					clearInterval(id);
					return;
				}
					
				x = x + 2;
			}
		}
		
		public function destroy() {
			var i:int = 0;
			var id:Number = setInterval(moveme, 20);
			function moveme() {
				if(i == 30)
					clearInterval(id);
				
				x = x - 5;
				i++;
			}
		}
		
		public function nextItem():String {
			remove(fields[current]);
			current = getNext();
			mark(fields[current]);
			return fields[current].text;
		}
		
		public function prevItem():String {
			remove(fields[current]);
			current = getPrev();
			mark(fields[current]);
			return fields[current].text;
		}
		
		private function getPrev():int {
			if(current == 0)
				return fields.length - 1;
			else
				return current - 1;
		}
		
		private function getNext():int {
			if(current == fields.length - 1)
				return 0;
			else
				return current + 1;
		}
		
		private function mark(f:TextField) {
			f.background = true;
			f.backgroundColor = 0x4B3AE7;
			//f.border = true;
		}
		
		private function remove(f:TextField) {
			f.background = false;
			f.backgroundColor = 0xFFFFFF;
		}
	}	
}